package com.hskn.hss.module.rptEmployeeWork.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.employeePunchCard.entity.EmployeePunchCard;
import com.hskn.hss.module.employeePunchCard.mapper.EmployeePunchCardMapper;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.module.rptEmployeeWork.Vo.RptEmployeeWorkVo;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.mapper.RptEmployeeWorkMapper;
import com.hskn.hss.module.rptEmployeeWork.service.RptEmployeeWorkService;
import com.hskn.hss.module.rptEmployeeWork.to.RptEmployeeWorkTo;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.tlopetation.mapper.TlOperationMapper;
import com.hskn.hss.module.tlopetation.service.ITlOperationService;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Service
public class RptEmployeeWorkServiceImpl extends ServiceImpl<RptEmployeeWorkMapper, RptEmployeeWork> implements RptEmployeeWorkService {

    @Resource
    private RptEmployeeWorkMapper rptEmployeeWorkMapper;
    @Resource
    private TlEmployeeMapper tlEmployeeMapper;
    @Resource
    private ITlOperationService operationService;
    @Resource
    private EmployeePunchCardMapper employeePunchCardMapper;
    @Resource
    private TlCarMapper carMapper;
    @Resource
    private RptCarWorkMapper rptCarWorkMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        QueryWrapper<RptEmployeeWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(RptEmployeeWork::getCreateTime);
        //        机构  日期  状态
        String employeeId = params.getString("employeeId");
        if (StringUtils.isNotEmpty(employeeId)) {
            queryWrapper.lambda().eq(RptEmployeeWork::getEmployeeId, employeeId);
        }
        String workDate = params.getString("workDate");
        if (StringUtils.isNotEmpty(workDate)) {
            queryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, workDate);
        }
        String status = params.getString("status");
        if (StringUtils.isNotEmpty(status)) {
            queryWrapper.lambda().eq(RptEmployeeWork::getStatus, status);
        }
        String planName = params.getString("planName");
        if (StringUtils.isNotEmpty(planName)) {
            queryWrapper.lambda().eq(RptEmployeeWork::getPlanName, planName);
        }
        String workMonth = params.getString("workMonth");
        if (StringUtils.isNotEmpty(workMonth)) {
            queryWrapper.lambda().eq(RptEmployeeWork::getWorkMonth, workMonth);
        }

        List<EmployeePunchCard> employeePunchCardList = employeePunchCardMapper.selectList(new QueryWrapper<>());
        Map<String, List<EmployeePunchCard>> employeePunchCardMap = employeePunchCardList.stream().collect(Collectors.groupingBy(EmployeePunchCard::getEmployeeId));

        queryWrapper.lambda().orderByAsc(RptEmployeeWork::getPlanName);
        queryWrapper.lambda().orderByAsc(RptEmployeeWork::getShiftName);
        List<RptEmployeeWork> list = rptEmployeeWorkMapper.selectList(queryWrapper);
        for (RptEmployeeWork rptEmployeeWork : list) {
            List<EmployeePunchCard> employeePunchCardList1 = employeePunchCardMap.get(rptEmployeeWork.getEmployeeId());
            if (null != employeePunchCardList1) {
                for (EmployeePunchCard employeePunchCard : employeePunchCardList1) {
                    rptEmployeeWork.setPunchCardStartTime(employeePunchCard.getWorkTime());
                    rptEmployeeWork.setPunchCardEndTime(employeePunchCard.getAfterWorkTime());
                }
            }
        }
        json.put("list", list);
        return json;
    }

    @Override
    public Set<RptEmployeeWorkVo> getRptEmployeeWorkList() {
        User user = LoginUtils.getUser();
        Set<RptEmployeeWorkVo> rptEmployeeWorkVoList = buildRptEmployeeWorkVoList(user);
        return rptEmployeeWorkVoList;
    }
    @Override
    public Set<RptEmployeeWorkVo> buildRptEmployeeWorkVoList(User user) {
        Set<RptEmployeeWorkVo> rptEmployeeWorkVoList = new HashSet<>();
        QueryWrapper<TlEmployee> tlEmployeeQueryWrapper = new QueryWrapper<>();
        tlEmployeeQueryWrapper.lambda().eq(TlEmployee::getBindaccount, user.getUid());
        TlEmployee tlEmployee = tlEmployeeMapper.selectOne(tlEmployeeQueryWrapper);

        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
        rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getEmployeeId, tlEmployee.getId());
        rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, DateUtil.format(new Date(), "yyyy-MM-dd"));
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        QueryWrapper<TlCar> carQueryWrapper = new QueryWrapper<>();
        carQueryWrapper.lambda().eq(TlCar::getBindaccount, user.getUid()).select(TlCar::getId,TlCar::getCarcategory).last(" limit 1");
        TlCar car= new TlCar();
        List<TlCar> carList=carMapper.selectList(carQueryWrapper);
        if (CollectionUtils.isNotEmpty(carList)) {
            car = carList.get(0);
        }
        QueryWrapper<RptCarWork> carWorkQueryWrapper=new QueryWrapper<>();
        carWorkQueryWrapper.lambda().eq(RptCarWork::getCarId,car.getId());
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, DateUtil.format(new Date(), "yyyy-MM-dd"));
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
        Map<String,TlOperation> tlOperationMap = operationService.getTlOperationMap(new QueryWrapper<>());
        for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList) {
            RptEmployeeWorkVo rptEmployeeWorkVo = new RptEmployeeWorkVo();
            rptEmployeeWorkVo.setGridId(rptEmployeeWork.getGridId());
            rptEmployeeWorkVo.setGridName(rptEmployeeWork.getGridName());
            if (tlOperationMap.containsKey(tlEmployee.getPersontype())) {
                rptEmployeeWorkVo.setWorkTypeId(tlEmployee.getPersontype());
            }
            if (tlOperationMap.containsKey(tlEmployee.getPersontype())) {
                rptEmployeeWorkVo.setWorkTypeName(tlOperationMap.get(tlEmployee.getPersontype()).getOperationtype());
            }
            rptEmployeeWorkVoList.add(rptEmployeeWorkVo);
        }
        for (RptCarWork rptCarWork : rptCarWorkList) {
            RptEmployeeWorkVo rptEmployeeWorkVo = new RptEmployeeWorkVo();
            rptEmployeeWorkVo.setGridId(rptCarWork.getGridId());
            rptEmployeeWorkVo.setGridName(rptCarWork.getGridName());
            if (tlOperationMap.containsKey(car.getCarcategory())) {
                rptEmployeeWorkVo.setWorkTypeId(car.getCarcategory());
            }
            if (tlOperationMap.containsKey(car.getCarcategory())) {
                rptEmployeeWorkVo.setWorkTypeName(tlOperationMap.get(car.getCarcategory()).getOperationtype());
            }
            rptEmployeeWorkVoList.add(rptEmployeeWorkVo);
        }
        return rptEmployeeWorkVoList;
    }

}

